﻿using System;
using System.Globalization;
using System.Windows;

namespace Gmantis.Controls
{
    [TemplateVisualState(Name = "InvalidFocused", GroupName = "ValidationStatesStates"), TemplateVisualState(Name = "Valid", GroupName = "ValidationStatesStates"), TemplateVisualState(Name = "InvalidUnfocused", GroupName = "ValidationStatesStates")]
    public class ValidationDecorator : System.Windows.Controls.Control
    {
        // Fields
        internal bool _isLoaded;
        public static readonly DependencyProperty CornerRadiusProperty = DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(ValidationDecorator), null);
        public static readonly DependencyProperty TargetProperty = DependencyProperty.Register("Target", typeof(FrameworkElement), typeof(ValidationDecorator), null);
        public static readonly DependencyProperty ValidationStateProperty = DependencyProperty.Register("ValidationState", typeof(ValidationState), typeof(ValidationDecorator), new PropertyMetadata(ValidationState.Valid, new PropertyChangedCallback(ValidationDecorator.OnValidationStatePropertyChanged)));

        // Methods
        public ValidationDecorator()
        {
            RoutedEventHandler handler = null;
            base.DefaultStyleKey = typeof(ValidationDecorator);
            if (handler == null)
            {
                handler = (param0, param1) => this.ChangeVisualStateValidationStates(false);
            }
            base.Loaded += handler;
            this.SetCustomDefaultValues();
        }

        protected void ChangeVisualStateValidationStates(bool useTransitions)
        {
            if (this.ValidationState == ValidationState.InvalidUnfocused)
            {
                VisualStateHelper.GoToState(this, "InvalidUnfocused", useTransitions);
            }
            if (this.ValidationState == ValidationState.InvalidFocused)
            {
                VisualStateHelper.GoToState(this, "InvalidFocused", useTransitions);
            }
            if (this.ValidationState == ValidationState.Valid)
            {
                VisualStateHelper.GoToState(this, "Valid", useTransitions);
            }
        }

        public override void OnApplyTemplate()
        {
            string str = string.Empty;
            base.OnApplyTemplate();
            this._isLoaded = true;
            if (!string.IsNullOrEmpty(str))
            {
                this._isLoaded = false;
                if ((base.Template != null) && !PlatformIndependent.IsInDesignMode(this))
                {
                    throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Template cannot be applied to ValidationDecorator.\nDetails: {0}", new object[] { str }));
                }
            }
            else
            {
                this.ChangeVisualStateValidationStates(false);
            }
        }

        private static void OnValidationStatePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as ValidationDecorator).ChangeVisualStateValidationStates(true);
        }

        private void SetCustomDefaultValues()
        {
        }

        // Properties
        public CornerRadius CornerRadius
        {
            get { return (CornerRadius)GetValue(CornerRadiusProperty); }
            set { SetValue(CornerRadiusProperty, value); }
        }

        public FrameworkElement Target
        {
            get { return (FrameworkElement)GetValue(TargetProperty); }
            set { SetValue(TargetProperty, value); }
        }

        public ValidationState ValidationState
        {
            get { return (ValidationState)GetValue(ValidationStateProperty); }
            set { SetValue(ValidationStateProperty, value); }
        }
    }

    public enum ValidationState
    {
        Valid,
        InvalidFocused,
        InvalidUnfocused
    }
}
